package com.cz.fra.service.impl;

import java.util.List;

import com.cz.fra.dao.AccountDao;
import com.cz.fra.dao.EntityNotFoundException;
import com.cz.fra.dao.PersistenceException;
import com.cz.fra.dao.impl.JdbcAccountDao;
import com.cz.fra.entity.User;
import com.cz.fra.service.AccountService;
import com.cz.fra.service.ServiceException;

public class AccountServiceImpl implements AccountService{

	
	AccountDao accountDao = new JdbcAccountDao();
	
	@Override
	public String addAccount(User newUser) throws ServiceException {
		String result = null;
		try {
			result = accountDao.addAccount(newUser);
		} catch (EntityNotFoundException e) {
			e.printStackTrace();
		} catch (PersistenceException e) {
			e.printStackTrace();
		}
		if(result.equalsIgnoreCase("success")){
			return "success";	
		}else {
			return "failed";	
		}
	}

	@Override
	public boolean addAccountValidation(User newUser) throws ServiceException {
		boolean userNameCanBeUsed = true;
		try {
			userNameCanBeUsed = accountDao.checkUserName(newUser);
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userNameCanBeUsed;
	}

	@Override
	public List<String> getUsernameList() throws ServiceException {
		List<String> usernameList = null;
		try {
			usernameList = accountDao.getUsernameList();
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return usernameList;
	}

	@Override
	public int getUserIdByUsername(String username) throws ServiceException {
		int userId = -1;
		try {
			userId = accountDao.getUserIdByUsername(username);
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userId;
	}

	@Override
	public List<User> getUserList() throws ServiceException {
		List<User> userList = null;
		try {
			userList = accountDao.getUserList();
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userList;
	}

	
}
